﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Toom.CellularAutomata.Model.Fast
{


	public unsafe class CAPointFast
	{
		public int x,y;

		#region Ctors
		public CAPointFast( int x, int y)
		{
			this.x = x;
			this.y = y;
		}
		public CAPointFast(CAPointFast p)
		{
			this.x = p.x;
			this.y = p.y;
		}
		#endregion

		#region Static
		public static CAPointFast AddVector(CAFieldFast field, CAPointFast p, CAVectorFast v)
		{
			CAPointFast p2 = new CAPointFast(p);
			p2.x = p.x + v.i;
			p2.y = p.y + v.j;

			// handle case of too far in positive direction
			if (p2.x >= field.sizeX) p2.x = p2.x % field.sizeX;
			if (p2.y >= field.sizeY) p2.y = p2.y % field.sizeY;

			// handle case of too far in negative direction
			while (p2.x < 0)
				p2.x = p2.x + field.sizeX;

			while (p2.y < 0)
				p2.y = p2.y + field.sizeY;

			return p2;
		}
		#endregion

	}

	public unsafe class CAVectorFast : CAExpressionFast
	{
		public int i,j;

		#region Ctor
		public CAVectorFast(int i, int j)
		{
			this.i = i;
			this.j = j;
		}
		#endregion


		public override int Evaluate(CAFieldFast f, CAPointFast p)
		{
			CAPointFast p2 = CAPointFast.AddVector(f, p, this);
			return f.GetDataAt(p2);
		}

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("CAVector({0},{1})",i,j);
			return sb.ToString();
		}
	}

	public abstract class CAExpressionFast
	{
		public static int MAX_ARRAY_SIZE = 8;
		public abstract int Evaluate(CAFieldFast f, CAPointFast p);

		public abstract override string ToString();
	
	}


}
